package com.zhongxi.guoyuan.widget.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.TextView;
import android.widget.Toast;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.view.MainHomeActivity;

/**
 * Created by Administrator on 2018/3/5.
 */

@SuppressLint("AppCompatCustomView")
public class CountTimerView extends TextView {
//    圆实心的颜色
    private int circSolidColor;
//    圆边框的颜色
    private int circFrameColor;
//    圆边框的宽度
    private int circFrameWidth=6;
//    圆的半径
    private int circRadius;
//    进度条的颜色
    private int progressColor;
//    进度条的宽度
    private int progressWidth=6;
//    文字的颜色
    private int textColor;

    private Paint mPaint;
    private Rect mBounds;
    private RectF mArcRectF;

    private int centerX;
    private int centerY;

    private String mtext="跳过";

    private long timeMillis=2000;
    private int progress=100;

    private OnProgressListener progressListener;
    public CountTimerView(Context context) {
        super(context);
        init();
    }

    public void setProgressColor(int progressColor) {
        this.progressColor = progressColor;
    }

    public CountTimerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
        TypedArray typedArray=getContext().obtainStyledAttributes(attrs, R.styleable.CountDownProgress);
        if (typedArray!=null){
            if (typedArray.hasValue(R.styleable.CountDownProgress_circSolidColor)){
                circSolidColor=typedArray.getColor(R.styleable.CountDownProgress_circSolidColor,0);
            }else {
                circSolidColor=typedArray.getColor(R.styleable.CountDownProgress_circSolidColor, Color.parseColor("#E6999999"));
            }

            if (typedArray.hasValue(R.styleable.CountDownProgress_circFrameColor)){
                circFrameColor=typedArray.getColor(R.styleable.CountDownProgress_circFrameColor,0);
            }else {
                circFrameColor=typedArray.getColor(R.styleable.CountDownProgress_circFrameColor, Color.parseColor("#A9A9A9"));
            }

            if (typedArray.hasValue(R.styleable.CountDownProgress_textColor)){
                textColor=typedArray.getColor(R.styleable.CountDownProgress_textColor,0);
            }else {
                textColor=typedArray.getColor(R.styleable.CountDownProgress_textColor, Color.parseColor("#ffffff"));
            }
            if (typedArray.hasValue(R.styleable.CountDownProgress_progressColor)){
                progressColor=typedArray.getColor(R.styleable.CountDownProgress_progressColor,0);
            }else {
                progressColor=typedArray.getColor(R.styleable.CountDownProgress_progressColor, Color.parseColor("#ffffff"));
            }
            typedArray.recycle();
        }
    }

   private void init(){

        mPaint=new Paint();
        mBounds=new Rect();
        mArcRectF=new RectF();
    }

    public enum ProgressType{
        COUNT,
        COUNT_BACK
    }

    private ProgressType mProgressType= ProgressType.COUNT_BACK;

    public void setmProgressType(ProgressType mProgressType) {
        this.mProgressType = mProgressType;
        resetProgress();
        invalidate();

    }



    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        if (width>height){
            height=width;
        }else {
            width=height;
        }

        circRadius=width/2;
        setMeasuredDimension(width,height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        getDrawingRect(mBounds);

        centerX=mBounds.centerX();
        centerY=mBounds.centerY();

//        画实心圆
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(circSolidColor);
        canvas.drawCircle(mBounds.centerX(),mBounds.centerY(),circRadius,mPaint);

//        画外边框
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(circFrameWidth);
        mPaint.setColor(circFrameColor);
        canvas.drawCircle(mBounds.centerX(),mBounds.centerY(),circRadius-circFrameWidth,mPaint);

        Paint text_paint=getPaint();
        text_paint.setColor(textColor);
        text_paint.setAntiAlias(true);
        text_paint.setTextAlign(Paint.Align.CENTER);
        float textY=centerY-(text_paint.descent()+text_paint.ascent())/2;
        canvas.drawText(mtext,centerX,textY,text_paint);

        mPaint.setColor(progressColor);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(progressWidth);
        mPaint.setStrokeCap(Paint.Cap.ROUND);


        mArcRectF.set(mBounds.left+progressWidth,mBounds.top+progressWidth,mBounds.right-progressWidth
        ,mBounds.bottom-progressWidth);

        // -360*progress/100 逆时针      360*progress/100顺时针
        canvas.drawArc(mArcRectF,-90,-360*progress/100,false,mPaint);
    }
    public interface OnProgressListener{
        /***
         * 进度通知
         * @param progress 进度值
         */
        void onProgress(int progress);
    }

    private Runnable progressChangerTask=new Runnable() {
        @Override
        public void run() {
            removeCallbacks(this);
            switch (mProgressType) {
                case COUNT:
                    progress-=1;
                    break;
                case COUNT_BACK:
                    progress+=1;
                    break;
            }

            if (progress>=0&&progress<=100){
                if (progressListener!=null)
                    progressListener.onProgress(progress);
                invalidate();
                postDelayed(progressChangerTask,timeMillis/60);
            }else
                progress=validateProgress(progress);
        }
    };

    public void start(){
        stop();
        post(progressChangerTask);
    }

    public void stop(){
        removeCallbacks(progressChangerTask);
    }
    public void reStart(){
        resetProgress();
        start();
    }

   private void resetProgress(){
        switch (mProgressType){
            case COUNT:
                progress=100;
                break;
            case COUNT_BACK:
                progress=0;
                break;
        }
    }

    private int validateProgress(int progress){
        if (progress>100)
            progress=100;
        else if (progress<0)
            progress=0;
        return progress;
    }

    /***
     * 设置文字
     * @param mtext
     */
    public void setMtext(String mtext) {
        this.mtext = mtext;

    }

    /***
     * 倒计时
     * @param timeMillis
     */
    public void setTimeMillis(long timeMillis) {
        this.timeMillis = timeMillis;
        invalidate();
    }

    /***
     * 进度监听
     * @param progressListener
     */
    public void setProgressListener(OnProgressListener progressListener) {
        this.progressListener = progressListener;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                int x= (int) event.getX();
                int y= (int) event.getY();
                if (Math.abs(x-(mBounds.centerX()))<=(circRadius)*2&& Math
                        .abs(y-(mBounds.centerY()))<=(circRadius)*2){
                    stop();
                    getContext().startActivity(new Intent(getContext(),MainHomeActivity.class));
                }
                break;
        }
        return super.onTouchEvent(event);
    }
}
